package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.logic.pb.GhostPb;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostActivity;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.LinkedList;

@Cmd(code = CrossProtocol.C2_GHOST_ACTIVITY_GET_GAME_DATA, desc = "跨服获取游戏数据")
public class CrossGhostGetGameDataCmd extends CrossCommand {

    private int activityId;
    private GhostActivityProto.GhostUserDataMsg userData;
    private long serverId;
    private long userId;
    private boolean needErrorCode = true;

    private GhostActivityProto.TodayGhostDataMsg.Builder todayGhostDataMsg = null;
    private CommonActivityProto.CommonActivityRankListRespMsg.Builder rankMsg = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.CrossGetGhostGameDataReqMsg reqMsg = GhostActivityProto.CrossGetGhostGameDataReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userData = reqMsg.getUserData();
        serverId = reqMsg.getServerId();
        userId = packet.getPlayerId();
        needErrorCode = reqMsg.getNeedErrorCode();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        // 获取排行榜
        rankMsg = CommonActivityProto.CommonActivityRankListRespMsg.newBuilder();
        rankMsg.setMyRank(-1);
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, eGamePlayerEventType.GhostScoreRank.getValue());
        if (rankListModel != null) {
            LinkedList<CrossUserRank> rankList = rankListModel.getRankList();
            rankMsg.setRankList(CrossRankPb.parseRankListMsg(rankList, 8));
            rankMsg.setMyRank(rankListModel.getMyRank(userId, serverId));
        } else {
            rankMsg.setRankList(RankProto.RankListMsg.newBuilder());
        }

        GhostActivity activity = GhostMgr.getActivity(activityId);
        if (activity == null || activity.getGhostData() == null || activity.getGhostConfig() == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        // 有生成过数据才有
        if (activity.getGhostData().getMaxBatchNo() > 0) {
            todayGhostDataMsg = GhostPb.parseTodayGhostDataMsg(activity.getGhostData(), activity.getGhostConfig());
        }
    }

    @Override
    public void sendData() {
        GhostActivityProto.GetGhostGameDataRespMsg.Builder builder = GhostActivityProto.GetGhostGameDataRespMsg.newBuilder();
        builder.setRet(needErrorCode ? ret : 0);
        builder.setUserData(userData);
        builder.setRankMsg(rankMsg);
        if (todayGhostDataMsg != null) {
            builder.setTodayGhostData(todayGhostDataMsg);
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_GHOST_GET_GAME_DATA, builder));
    }
}
